package com.example.aircraftwar2024.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.aircraftwar2024.ActivityManager;
import com.example.aircraftwar2024.game.BaseGame;
import com.example.aircraftwar2024.game.EasyGame;
import com.example.aircraftwar2024.game.HardGame;
import com.example.aircraftwar2024.game.MediumGame;

import java.sql.Time;


public class GameActivity extends AppCompatActivity {
    private static final String TAG = "GameActivity";

    private int gameMode;
    public static int screenWidth,screenHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityManager activityManager= ActivityManager.getActivityManager();
        activityManager.addActivity(this);

        getScreenHW();

        if(getIntent() != null){
            gameMode = getIntent().getIntExtra("gameMode",0);
        }

        /*TODO:根据用户选择的难度加载相应的游戏界面*/
        BaseGame baseGameView = null;
        if(gameMode == 0) {
            baseGameView = new EasyGame(this,mHandler);
        } else if(gameMode == 1) {
            baseGameView = new MediumGame(this,mHandler);
        } else if(gameMode == 2) {
            baseGameView = new HardGame(this,mHandler);
        }
        setContentView(baseGameView);
    }

    //主线程handler
    public Mhandler mHandler = new Mhandler(Looper.getMainLooper());
    class Mhandler extends Handler {
        public Mhandler(Looper mainLooper) {
            super(mainLooper);
        }
        // 通过复写handlerMessage() 从而确定更新UI的操作
        @Override
        public void handleMessage(Message msg) {
            // 根据 Message对象的what属性 标识不同的消息
            Intent intent;
            //跳转至RecordActivity
            intent=new Intent(GameActivity.this, RecordActivity.class);
            switch (msg.what) {
                case 0: //简单
                    intent.putExtra("gameMode",0);
                    break;
                case 1: //中等
                    intent.putExtra("gameMode",1);
                    break;
                case 2: //困难
                    intent.putExtra("gameMode",2);
                    break;
            }

            //联机游戏未结束，不跳转页面
            if (MainActivity.connGameOverFlag){
                intent.putExtra("score",BaseGame.score);
                startActivity(intent);
            }
        }
    }

    public void getScreenHW(){
        //定义DisplayMetrics 对象
        DisplayMetrics dm = new DisplayMetrics();
        //取得窗口属性
        getDisplay().getRealMetrics(dm);

        //窗口的宽度
        screenWidth= dm.widthPixels;
        //窗口高度
        screenHeight = dm.heightPixels;

        Log.i(TAG, "screenWidth : " + screenWidth + " screenHeight : " + screenHeight);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}